<?php
include('../../../includes/classes/core.php');



$start = $_REQUEST['start'];
$end   = $_REQUEST['end'];
$action = $_REQUEST['act'];
$name = $_REQUEST['name'];
$error = '';
$data = '';


switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    
    	    $rResult = mysql_query("SELECT 		production_category.id,
										production_category.`name` AS `category_name`,
										ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2) AS `money`,
										SUM(sale_transaction_detail.product_quantity) AS `qnt`
										FROM sale_transaction
										LEFT JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
										LEFT JOIN production ON production.id = sale_transaction_detail.production_id
										LEFT JOIN production_category ON production.pos_menu_category = production_category.id
										WHERE production_category.`name` IS NOT NULL AND `sale_transaction`.`status`=0 AND DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end'
										GROUP BY production.`pos_menu_category`");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );

	        break;

case 'get_prod_list':
	        	$count = $_REQUEST['count'];
	        	$name  = $_REQUEST['name'];
	        	$rResult = mysql_query("SELECT  production.id,
	        					production.`name` AS `prod_name`,
	        					ROUND(SUM(sale_transaction_detail.product_quantity*sale_transaction_detail.cost),2) AS `money`,
								SUM(sale_transaction_detail.product_quantity) AS `quantity`						
								FROM sale_transaction
								LEFT JOIN sale_transaction_detail ON sale_transaction.id = sale_transaction_detail.transaction_id
								LEFT JOIN production ON production.id = sale_transaction_detail.production_id
								LEFT JOIN production_category ON production.pos_menu_category = production_category.id
								WHERE production_category.`name` = '$name' AND `sale_transaction`.`status`=0 AND DATE(`sale_transaction`.`end_date`) >= '$start' AND DATE(`sale_transaction`.`end_date`) <= '$end'
								GROUP BY production.`name`");
	        				
	        			$output = array(
	        					"aaData"	=> array()
	        			);
	        				
	        			while ( $aRow = mysql_fetch_array( $rResult ) )
	        			{
	        			$row = array();
	        			for ( $i = 0 ; $i < $count ; $i++ )
	        			{
	        			/* General output */
	        				$row[] = $aRow[$i];
	        			}
	        			$output['aaData'][] = $row;
	        			}
	        				
	        			echo json_encode( $output );
	        
	        break;
	    default:
	       	echo "null";
	}

?>